package com.qingfeng.core.dao.qingfeng;

import com.github.pagehelper.PageInfo;
import com.qingfeng.core.config.database.BaseMapper;
import com.qingfeng.core.dao.BaseDao;
import com.qingfeng.core.entity.qingfeng.User;
import com.qingfeng.core.mapper.qingfeng.UserMapper;
import com.qingfeng.core.utils.DesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

/**
 * @description: 用户dao
 * @author:LWY
 * @date: 2017/10/16 17:15
 * @modified:
 */

@Repository
public class UserDao extends BaseDao<User> {

    @Autowired
    UserMapper userMapper;

    @Override
    protected BaseMapper<User> getBaseMapper() {
        return userMapper;
    }

    /**
     * 获取用户信息
     *
     * @param phone 手机
     * @param pwd   密码
     * @return
     */

    public User get(String phone, String pwd) {
        User user = new User();
        user.setPhone(phone);
        user.setPwd(pwd);
        return userMapper.selectOne(user);
    }

    /**
     * 获取用户信息
     *
     * @param phone 手机
     * @return
     */
    public User get(String phone) {
        User user = new User();
        user.setPhone(phone);
        return userMapper.selectOne(user);
    }

    /**
     * 分页获取用户列表
     *
     * @param pageNum   页码
     * @param pageSize  每页条数
     * @param phone     手机
     * @param nick_name 昵称
     * @param state     用户状态（1:可用，2：禁用）
     * @return
     */

    public PageInfo<User> get(Integer pageNum, Integer pageSize, String phone, String nick_name, String state) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(nick_name)) {
            criteria.andLike("nick_name", "%" + nick_name + "%");
        }
        if (StringUtils.isNotEmpty(phone)) {
            criteria.andLike("phone", "%" + phone + "%");
        }
        if (StringUtils.isNotEmpty(state)) {
            criteria.andCondition("state=", state);
        }
        return super.get(pageNum, pageSize, example);
    }


    /**
     * 修改密码
     *
     * @param phone 手机
     * @param pwd   新密码
     */
    public void forget(String phone, String pwd) {
        pwd = DesUtil.encryptBasedDes(pwd);
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        User user = new User();
        user.setPwd(pwd);
        userMapper.updateByExampleSelective(user, example);
    }


    /**
     * 绑定手机
     *
     * @param phone 手机
     * @param id    id
     */
    public void bindPhone(String phone, String id) {
        User user = new User();
        user.setId(id);
        user.setPhone(phone);
        userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 保存用户信息
     *
     * @param id        用户id
     * @param nick_name 昵称
     * @param sex       性别
     * @param grade_id  年级id
     * @param major     专业
     * @param specialty 特长
     */
    public void saveInfo(String id, String nick_name, Integer sex, String grade_id, String major, String specialty) {
        User user = new User();
        user.setId(id);
        if (StringUtils.isNotEmpty(nick_name)) {
            user.setNick_name(nick_name);
        }
        if (sex != 0) {
            user.setSex(sex);
        }
        if (StringUtils.isNotEmpty(grade_id)) {
            user.setGrade(grade_id);
        }
        if (StringUtils.isNotEmpty(major)) {
            user.setMajor(major);
        }
        if (StringUtils.isNotEmpty(specialty)) {
            user.setSpecialty(specialty);
        }
        userMapper.updateByPrimaryKeySelective(user);
    }
}
